package controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.UserAccount;
import util.Log;
import validator.UserAccountValidator;
import web.JSONMessage;
import db.ImbiConnection;

public class UserController extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if(request.getSession().getAttribute("user") != null){
			int edit;
			try{
				edit = Integer.parseInt(request.getParameter("edit"));
				if(edit <= 0) throw new NumberFormatException();
				handleEdit(request, response, edit);
			} catch(NumberFormatException | NullPointerException e){
				handleViewAll(request, response);
			}						
		} else {
			request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
		}				
	}

	private void handleViewAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("users", ImbiConnection.getUserAccountDAO().findAll());
		request.setAttribute("employees", ImbiConnection.getEmployeeDAO().findAll());
		request.getRequestDispatcher("/WEB-INF/user.jsp").forward(request, response);
	}

	private void handleEdit(HttpServletRequest request,
			HttpServletResponse response, int edit) throws ServletException,
			IOException {
		request.setAttribute("employees", ImbiConnection.getEmployeeDAO().findAll());
		request.setAttribute("user", ImbiConnection.getUserAccountDAO().find(edit));
		if(request.getAttribute("user") != null){
			request.getRequestDispatcher("/WEB-INF/useredit.jsp").forward(request, response);
		} else {
			request.getRequestDispatcher("/WEB-INF/user.jsp").forward(request, response);
		}
	}	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {		
		UserAccount u = (UserAccount) request.getSession().getAttribute("user");
			if(u != null){
			UserAccountValidator validator = new UserAccountValidator(
				request.getParameter("id"),
				request.getParameter("employee"),
				request.getParameter("status"),
				request.getParameter("valid_from"),
				request.getParameter("valid_to"),
				request.getParameter("password_never_expires"),
				"password", // satisfy validator
				"user" // satisfy validator
			);
			
			if(validator.isValidUserUpdate()){
				UserAccount user = ImbiConnection.getUserAccountDAO().find(validator.getId());
				if(user != null){
					user.setSubject(validator.getSubject());
					user.setStatus(validator.getStatus());
					user.setValidFrom(validator.getValidFrom());
					user.setValidTo(validator.getValidTo());
					user.setPasswordNeverExpires(validator.getPasswordNeverExpires());
					ImbiConnection.getUserAccountDAO().update(user);
					response.getWriter().write(JSONMessage.message("Update successful"));
					Log.write(u.getUsername()+" updated user "+user.getUsername());	
				}
			} else {
				request.setAttribute("validator", validator);			
				response.getWriter().write(JSONMessage.errorMessage("Illegal parameters"));
			}
		}
		
	}
}